<template>
  <div>
    <div v-model="searchData" style="margin: 10px 0">
      <el-input v-model="searchData.bookName" style="width: 200px" placeholder="请输入书名"
                suffix-icon="el-icon-search"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="rest">重置</el-button>
    </div>
    <el-button type="danger" @click="delBatch">批量删除 <i class="el-icon-remove-outline"></i></el-button>
    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="borrowId" label="借阅号"></el-table-column>
      <el-table-column prop="bookName" label="书名"></el-table-column>
      <el-table-column prop="borrowTime" label="借阅时间"></el-table-column>
      <el-table-column prop="returnTime" label="归还时间"></el-table-column>
      <el-table-column prop="num" label="借阅次数"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.statusCom===0" @click="add(scope.row)">评分 <i class="el-icon-remove-outline"></i></el-button>
          <el-button type="primary" v-if="scope.row.statusSpk===0" @click="speak(scope.row)">评论 <i class="el-icon-remove-outline"></i></el-button>
          <el-button type="primary" @click="del(scope.row,1)">删除 <i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="评分" :visible.sync="dialogFormVisible" width="30%">
       <el-rate
           v-model="score"
           :colors="colors">
       </el-rate>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
    </el-dialog>
    <el-dialog title="评价" :visible.sync="dialogSpeakVisible" width="30%">
      <div style="border-bottom: 1px solid orangered; padding: 10px 0; font-size: 20px">评论</div>
      <div style="padding: 10px 0">
        <el-input size="small" type="textarea" v-model="commentForm.content"></el-input>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSpeakVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveSpeak">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "HistoryOder",
  data() {
    return {
      scoreData: {
        borrowId:'',
        bookId:0,
        score: 0,
      },
      commentForm: {
        content: '',
        bookId: 0,
        contentReply:''
      },
      score: 0,
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      multipleSelection: [],
      form: {},
      info: {},
      tableData: [],
      searchOptions: [],
      headerBg: 'headerBg',
      dialogFormVisible: false,
      dialogSpeakVisible:false,
      current: 0,
      total: 0,
      pageSize: 5,
      options: [],
      msg: '',
      searchData: {
        bookName: '',
        nickName: '',
        name: ''
      },
      borrowUpId:''
    }
  },
  created() {
    this.load()
  },
  methods: {
    speak(row){
      this.commentForm.bookId = row.bookId
      this.borrowUpId = row.borrowId
      this.dialogSpeakVisible=true
    },
    saveSpeak(){
      if (!this.user.id) {
        this.$message.warning("请登录后重试")
        return
      }
      // this.commentForm.bookId = this.bookId
      if (this.commentForm.contentReply) {
        this.commentForm.content = this.commentForm.contentReply
      }
      this.request.post("/comment/save", this.commentForm).then(res => {
        if (res.code === 200) {
          this.commentForm.content = ''
          this.$message.success("评论成功")
          this.dialogSpeakVisible = false
          this.updateSta()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    updateSta(){
      this.request.post("/history/update/status/"+this.borrowUpId)
    },
    add(row) {
      this.score=0
      this.dialogFormVisible = true
      this.scoreData.bookId = row.bookId
      this.scoreData.borrowId = row.borrowId
      this.scoreData.score = this.score
    },
    save() {
      this.scoreData.score = this.score
      this.request.get("/score/add", {
        params: {
         bookId : this.scoreData.bookId,
         borrowId : this.scoreData.borrowId,
         score :this.scoreData.score
        }
      }).then(res => {
        this.dialogFormVisible = false
        if (res.code === 200) {
          this.$message.success("评分成功")
          this.load()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    changeUser() {
      this.load()
    },
    rest() { //重置搜索栏
      this.searchData.nickName = ''
      this.searchData.bookName = ''
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection.map(v => v.id))
    },

    del(row) {
      this.$confirm('确定删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/history/delete/" + row.id + "/" + 1).then(resp => {
          if (resp.code === 200) {
            this.load()
            this.$message({
              type: 'success',
              message: '取消删除!',
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    addHander() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleSizeChange(pageSize) {
      console.log("每页条数: " + pageSize)
      this.pageSize = pageSize;
      this.load()
    },
    handleCurrentChange(current) {
      console.log("当前页码: " + current)
      this.current = current;
      this.load()
    },
    collapse() {  // 点击收缩按钮触发
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {  // 收缩
        this.sideWidth = 64
        this.collapseBtnClass = 'el-icon-s-unfold'
        this.logoTextShow = false
      } else {   // 展开
        this.sideWidth = 200
        this.collapseBtnClass = 'el-icon-s-fold'
        this.logoTextShow = true
      }
    },
    load() {
      this.request.get("/history/page", {
            params: {
              current: this.current,
              pageSize: this.pageSize,
              bookName: this.searchData.bookName,
              nickName: this.searchData.nickName
            }
          }
      ).then(resp => {
        console.log(resp)
        this.tableData = resp.data.records
        this.total = resp.data.total
      })
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  //[1,2,3]
      this.$confirm('此操作将删除这些记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/history/delete/batch/" + 1 + "/" + ids).then(resp => {
          if (resp.code === 200) {
            this.load()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },


  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
</style>